<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>MongoDB 备忘清单
 &#x26;  mongodb cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="MongoDB 备忘清单
===

MongoDB 此备忘单包含一些方便的提示、命令和快速参考，可让您立即连接并进行 CRUD

入门，为开发人员分享快速参考备忘单。">
<meta keywords="mongodb,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="..\style\style.css">
<link rel="stylesheet" href="..\style\katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="..\index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/mongodb.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.4.1"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="mongodb-备忘清单"><svg viewBox="0 0 120 257" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="M82.323 28.644C71.537 15.847 62.249 2.85 60.35.15c-.2-.2-.5-.2-.7 0-1.897 2.7-11.185 15.697-21.971 28.494C-54.902 146.721 52.26 226.406 52.26 226.406l.9.6C53.958 239.303 55.955 257 55.955 257h7.99s1.998-17.597 2.797-29.994l.899-.7c.1.1 107.263-79.585 14.68-197.662ZM59.95 224.606s-4.793-4.099-6.092-6.198v-.201l5.793-128.574c0-.4.6-.4.6 0l5.792 128.574v.201c-1.299 2.099-6.093 6.198-6.093 6.198Z" fill="currentColor"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#mongodb-备忘清单"><span class="icon icon-link"></span></a>MongoDB 备忘清单</h1><div class="wrap-body">
<p><a href="https://www.mongodb.com/developer/products/mongodb/cheat-sheet/#connect-mongodb-shell">MongoDB</a> 此备忘单包含一些方便的提示、命令和快速参考，可让您立即连接并进行 CRUD</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#连接-mongodb-shell">连接 MongoDB Shell</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#显示数据库">显示数据库</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#切换数据库">切换数据库</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#显示集合">显示集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运行-javascript-文件">运行 JavaScript 文件</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#crud">CRUD</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建">创建</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#寻找文件">寻找文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用运算符查找文档">使用运算符查找文档</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#读取">读取</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#更新">更新</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除">删除</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#数据库和集合">数据库和集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#drop">Drop</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建集合">创建集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#其他采集功能">其他采集功能</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#索引">索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表索引">列表索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建索引">创建索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除索引">删除索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#隐藏取消隐藏索引">隐藏/取消隐藏索引</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#方便的命令">方便的命令</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#"> </a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#各种各样的">各种各样的</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#改变流">改变流</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#分片集群">分片集群</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#副本集">副本集</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="连接-mongodb-shell"><a aria-hidden="true" tabindex="-1" href="#连接-mongodb-shell"><span class="icon icon-link"></span></a>连接 MongoDB Shell</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">mongo <span class="token comment"># 默认连接到 mongodb://127.0.0.1:27017</span>
</span><span class="code-line">mongo <span class="token parameter variable">--host</span> <span class="token operator">&#x3C;</span>host<span class="token operator">></span> <span class="token parameter variable">--port</span> <span class="token operator">&#x3C;</span>port<span class="token operator">></span> <span class="token parameter variable">-u</span> <span class="token operator">&#x3C;</span>user<span class="token operator">></span> <span class="token parameter variable">-p</span> <span class="token operator">&#x3C;</span>pwd<span class="token operator">></span> <span class="token comment"># 如果需要提示，请省略密码</span>
</span><span class="code-line">mongo <span class="token string">"mongodb://192.168.1.1:27017"</span>
</span><span class="code-line"><span class="token comment"># MongoDB 地图集</span>
</span><span class="code-line">mongo <span class="token string">"mongodb+srv://cluster-name.abcde.mongodb.net/&#x3C;dbname>"</span> <span class="token parameter variable">--username</span> <span class="token operator">&#x3C;</span>username<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="显示数据库"><a aria-hidden="true" tabindex="-1" href="#显示数据库"><span class="icon icon-link"></span></a>显示数据库</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">show dbs
</span><span class="code-line">db <span class="token comment">// 打印当前数据库</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="切换数据库"><a aria-hidden="true" tabindex="-1" href="#切换数据库"><span class="icon icon-link"></span></a>切换数据库</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">use <span class="token operator">&#x3C;</span>database_name<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="显示集合"><a aria-hidden="true" tabindex="-1" href="#显示集合"><span class="icon icon-link"></span></a>显示集合</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">show collections
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="运行-javascript-文件"><a aria-hidden="true" tabindex="-1" href="#运行-javascript-文件"><span class="icon icon-link"></span></a>运行 JavaScript 文件</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line"><span class="token function">load</span><span class="token punctuation">(</span><span class="token string">"myScript.js"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="crud"><a aria-hidden="true" tabindex="-1" href="#crud"><span class="icon icon-link"></span></a>CRUD</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="创建"><a aria-hidden="true" tabindex="-1" href="#创建"><span class="icon icon-link"></span></a>创建</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">insertOne</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">insert</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span><span class="token string">"Alex"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 批量插入</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">insert</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span><span class="token string">"Alex"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">ordered</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 无序批量插入</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token property">date</span><span class="token operator">:</span> <span class="token builtin keyword">ISODate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">insert</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"writeConcern"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"w"</span><span class="token operator">:</span> <span class="token string">"majority"</span><span class="token punctuation">,</span> <span class="token string-property property">"wtimeout"</span><span class="token operator">:</span> <span class="token number">5000</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="寻找文件"><a aria-hidden="true" tabindex="-1" href="#寻找文件"><span class="icon icon-link"></span></a>寻找文件</h3><div class="wrap-body">

























<table><thead><tr><th align="left">Commands</th><th>Description</th></tr></thead><tbody><tr><td align="left"><code>db.docx.findOne()</code></td><td>查找一个随机文档</td></tr><tr><td align="left"><code>db.docx.find().prettyPrint()</code></td><td>查找所有文档</td></tr><tr><td align="left"><code>db.docx.find({}, {name:true, _id:false})</code></td><td>仅显示文档 Docx 的名称</td></tr><tr><td align="left"><code>db.docx.find({}, {name:true, _id:false})</code></td><td>可以在多个文件中按属性查找一个文件</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="使用运算符查找文档"><a aria-hidden="true" tabindex="-1" href="#使用运算符查找文档"><span class="icon icon-link"></span></a>使用运算符查找文档</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->













































<table><thead><tr><th align="left">Operator</th><th>Description</th><th>Commands</th></tr></thead><tbody><tr><td align="left"><code>$gt</code></td><td>大于</td><td><code>db.docx.find({class:{$gt:'T'}</code></td></tr><tr><td align="left"><code>$gte</code></td><td>大于等于</td><td><code>db.docx.find({class:{$gt:'T'}</code></td></tr><tr><td align="left"><code>$lt</code></td><td>小于</td><td><code>db.docx.find({class:{$lt:'T'}</code></td></tr><tr><td align="left"><code>$lte</code></td><td>小于等于</td><td><code>db.docx.find({class:{$lte:'T'}</code></td></tr><tr><td align="left"><code>$exists</code></td><td>属性是否存在</td><td><code>db.docx.find({class:{$gt:'T'}</code></td></tr><tr><td align="left"><code>$regex</code></td><td>正则表达式匹配</td><td><code>db.docx.find({name:{$regex:'^USS\\sE'}})</code></td></tr><tr><td align="left"><code>$type</code></td><td>按元素类型搜索</td><td><code>db.docx.find({name : {$type:4}})</code></td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="读取"><a aria-hidden="true" tabindex="-1" href="#读取"><span class="icon icon-link"></span></a>读取</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">findOne</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 返回单个文档</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span>    <span class="token comment">// 返回一个游标 - 显示 20 个结果 - "it" 显示更多</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">pretty</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">,</span> <span class="token property">age</span><span class="token operator">:</span> <span class="token number">32</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 隐式逻辑“与”。</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">date</span><span class="token operator">:</span> <span class="token builtin keyword">ISODate</span><span class="token punctuation">(</span><span class="token string">"2020-09-25T13:57:17.180Z"</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 或“queryPlanner”或“allPlansExecution”</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">,</span> <span class="token property">age</span><span class="token operator">:</span> <span class="token number">32</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">explain</span><span class="token punctuation">(</span><span class="token string">"executionStats"</span><span class="token punctuation">)</span> 
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">distinct</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 数数</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">count</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">age</span><span class="token operator">:</span> <span class="token number">32</span><span class="token punctuation">}</span><span class="token punctuation">)</span>          <span class="token comment">// 基于馆藏元数据的估计</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">estimatedDocumentCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// 基于馆藏元数据的估计</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">countDocuments</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">age</span><span class="token operator">:</span> <span class="token number">32</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 聚合管道的别名 - 准确计数</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Comparison 比较</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$gt</span></span><span class="token operator">:</span> <span class="token number">1970</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$gte</span></span><span class="token operator">:</span> <span class="token number">1970</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$lt</span></span><span class="token operator">:</span> <span class="token number">1970</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$lte</span></span><span class="token operator">:</span> <span class="token number">1970</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$ne</span></span><span class="token operator">:</span> <span class="token number">1970</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$in</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">1958</span><span class="token punctuation">,</span> <span class="token number">1959</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$nin</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">1958</span><span class="token punctuation">,</span> <span class="token number">1959</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Logical 逻辑</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span><span class="token punctuation">{</span><span class="token property"><span class="token keyword">$not</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$eq</span></span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property"><span class="token keyword">$or</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span> <span class="token operator">:</span> <span class="token number">1958</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"year"</span> <span class="token operator">:</span> <span class="token number">1959</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property"><span class="token keyword">$nor</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token property">price</span><span class="token operator">:</span> <span class="token number">1.99</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">sale</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token property"><span class="token keyword">$and</span></span><span class="token operator">:</span> <span class="token punctuation">[</span>
</span><span class="code-line">    <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$or</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token property">qty</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$lt</span></span> <span class="token operator">:</span><span class="token number">10</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">qty</span> <span class="token operator">:</span><span class="token punctuation">{</span><span class="token property"><span class="token keyword">$gt</span></span><span class="token operator">:</span> <span class="token number">50</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$or</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token property">sale</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">price</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$lt</span></span><span class="token operator">:</span> <span class="token number">5</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Element 元素</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$exists</span></span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"zipCode"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$type</span></span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"zipCode"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$type</span></span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Aggregation Pipeline 聚合管道</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">aggregate</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$match</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">status</span><span class="token operator">:</span> <span class="token string">"A"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$group</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">_id</span><span class="token operator">:</span> <span class="token string">"$cust_id"</span><span class="token punctuation">,</span> <span class="token property">total</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$sum</span></span><span class="token operator">:</span> <span class="token string">"$amount"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$sort</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">total</span><span class="token operator">:</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 使用“文本”索引进行文本搜索</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property"><span class="token keyword">$text</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">$search</span><span class="token operator">:</span> <span class="token string">"cake"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">score</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$meta</span></span><span class="token operator">:</span> <span class="token string">"textScore"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token punctuation">.</span><span class="token method function property-access">sort</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">score</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$meta</span></span><span class="token operator">:</span> <span class="token string">"textScore"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Regex 正则表达式</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex"><span class="token anchor function">^</span>Max</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">}</span><span class="token punctuation">)</span>   <span class="token comment">// 正则表达式：以字母“M”开头</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex"><span class="token anchor function">^</span>Max<span class="token anchor function">$</span></span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 正则表达式不区分大小写</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Array</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">tags</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$all</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"Realm"</span><span class="token punctuation">,</span> <span class="token string">"Charts"</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">field</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$size</span></span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 无法索引 - 更喜欢存储数组的大小并更新它</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">results</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$elemMatch</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">product</span><span class="token operator">:</span> <span class="token string">"xyz"</span><span class="token punctuation">,</span> <span class="token property">score</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$gte</span></span><span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Projections 预测</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"x"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"actors"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>               <span class="token comment">// actors + _id</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"x"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"actors"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">)</span>     <span class="token comment">// actors</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"x"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"actors"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string-property property">"summary"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 除了“actors”和“summary”之外的所有内容</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Sort 排序, skip 跳过, limit 限制</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">sort</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-property property">"rating"</span><span class="token operator">:</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">skip</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">limit</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Read Concern 阅读关注</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">readConcern</span><span class="token punctuation">(</span><span class="token string">"majority"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="更新"><a aria-hidden="true" tabindex="-1" href="#更新"><span class="icon icon-link"></span></a>更新</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token number">2016</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 警告！ 替换整个文档</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$set</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token number">2016</span><span class="token punctuation">,</span> <span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$unset</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$rename</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token string">"date"</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$inc</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$mul</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">price</span><span class="token operator">:</span> <span class="token builtin keyword">NumberDecimal</span><span class="token punctuation">(</span><span class="token string">"1.25"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token property">qty</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$min</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"imdb"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$max</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"imdb"</span><span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$currentDate</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"lastModified"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$currentDate</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"lastModified"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$type</span></span><span class="token operator">:</span> <span class="token string">"timestamp"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Array</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$push</span></span> <span class="token operator">:</span><span class="token punctuation">{</span><span class="token string-property property">"array"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$pull</span></span> <span class="token operator">:</span><span class="token punctuation">{</span><span class="token string-property property">"array"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$addToSet</span></span> <span class="token operator">:</span><span class="token punctuation">{</span><span class="token string-property property">"array"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$pop</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"array"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token comment">// 最后一个元素</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$pop</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"array"</span><span class="token operator">:</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 第一个元素</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$pullAll</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"array"</span> <span class="token operator">:</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$push</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">scores</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$each</span></span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">90</span><span class="token punctuation">,</span> <span class="token number">92</span><span class="token punctuation">,</span> <span class="token number">85</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">updateOne</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-property property">"grades"</span><span class="token operator">:</span> <span class="token number">80</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$set</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"grades.$"</span><span class="token operator">:</span> <span class="token number">82</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">updateMany</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$inc</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"grades.$[]"</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$set</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"grades.$[element]"</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">multi</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token property">arrayFilters</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string-property property">"element"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$gte</span></span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 更新很多</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token number">1999</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$set</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"decade"</span><span class="token operator">:</span> <span class="token string">"90's"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"multi"</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">updateMany</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"year"</span><span class="token operator">:</span> <span class="token number">1999</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$set</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"decade"</span><span class="token operator">:</span> <span class="token string">"90's"</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// FindOneAndUpdate 查找并更新</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">findOneAndUpdate</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$inc</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"points"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">returnNewDocument</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Upsert 更新插入</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$set</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">item</span><span class="token operator">:</span> <span class="token string">"apple"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token property"><span class="token keyword">$setOnInsert</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">defaultQty</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">upsert</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Replace 代替</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">replaceOne</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"firstname"</span><span class="token operator">:</span> <span class="token string">"Maxime"</span><span class="token punctuation">,</span> <span class="token string-property property">"surname"</span><span class="token operator">:</span> <span class="token string">"Beugnet"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Save 保存</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">save</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"item"</span><span class="token operator">:</span> <span class="token string">"book"</span><span class="token punctuation">,</span> <span class="token string-property property">"qty"</span><span class="token operator">:</span> <span class="token number">40</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Write concern 写关注</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">update</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$set</span></span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"x"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"writeConcern"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"w"</span><span class="token operator">:</span> <span class="token string">"majority"</span><span class="token punctuation">,</span> <span class="token string-property property">"wtimeout"</span><span class="token operator">:</span> <span class="token number">5000</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="删除"><a aria-hidden="true" tabindex="-1" href="#删除"><span class="icon icon-link"></span></a>删除</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">remove</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">remove</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">justOne</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">remove</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 警告！删除所有文档但不删除集合本身及其索引定义</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">remove</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">name</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">"writeConcern"</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"w"</span><span class="token operator">:</span> <span class="token string">"majority"</span><span class="token punctuation">,</span> <span class="token string-property property">"wtimeout"</span><span class="token operator">:</span> <span class="token number">5000</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">findOneAndDelete</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token string">"Max"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="数据库和集合"><a aria-hidden="true" tabindex="-1" href="#数据库和集合"><span class="icon icon-link"></span></a>数据库和集合</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="drop"><a aria-hidden="true" tabindex="-1" href="#drop"><span class="icon icon-link"></span></a>Drop</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line"><span class="token comment">// 删除集合及其索引定义</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">drop</span><span class="token punctuation">(</span><span class="token punctuation">)</span>    
</span><span class="code-line"><span class="token comment">// 仔细检查你*不*在 PROD 集群上......:-)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">dropDatabase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="创建集合"><a aria-hidden="true" tabindex="-1" href="#创建集合"><span class="icon icon-link"></span></a>创建集合</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line"><span class="token comment">// 使用 $jsonschema 创建集合</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">createCollection</span><span class="token punctuation">(</span><span class="token string">"contacts"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
</span><span class="code-line">   <span class="token property">validator</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$jsonSchema</span></span><span class="token operator">:</span> <span class="token punctuation">{</span>
</span><span class="code-line">      <span class="token property">bsonType</span><span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
</span><span class="code-line">      <span class="token property">required</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"phone"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">      <span class="token property">properties</span><span class="token operator">:</span> <span class="token punctuation">{</span>
</span><span class="code-line">         <span class="token property">phone</span><span class="token operator">:</span> <span class="token punctuation">{</span>
</span><span class="code-line">            <span class="token property">bsonType</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">,</span>
</span><span class="code-line">            <span class="token property">description</span><span class="token operator">:</span> <span class="token string">"必须是一个字符串并且是必需的"</span>
</span><span class="code-line">         <span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">         <span class="token property">email</span><span class="token operator">:</span> <span class="token punctuation">{</span>
</span><span class="code-line">            <span class="token property">bsonType</span><span class="token operator">:</span> <span class="token string">"string"</span><span class="token punctuation">,</span>
</span><span class="code-line">            <span class="token property">pattern</span><span class="token operator">:</span> <span class="token string">"@mongodb\.com$"</span><span class="token punctuation">,</span>
</span><span class="code-line">            <span class="token property">description</span><span class="token operator">:</span> <span class="token string">"必须是字符串并匹配正则表达式模式"</span>
</span><span class="code-line">         <span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">         <span class="token property">status</span><span class="token operator">:</span> <span class="token punctuation">{</span>
</span><span class="code-line">            <span class="token property">enum</span><span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">"Unknown"</span><span class="token punctuation">,</span> <span class="token string">"Incomplete"</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">            <span class="token property">description</span><span class="token operator">:</span> <span class="token string">"只能是枚举值之一"</span>
</span><span class="code-line">         <span class="token punctuation">}</span>
</span><span class="code-line">      <span class="token punctuation">}</span>
</span><span class="code-line">   <span class="token punctuation">}</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="其他采集功能"><a aria-hidden="true" tabindex="-1" href="#其他采集功能"><span class="icon icon-link"></span></a>其他采集功能</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">stats</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">storageSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">totalIndexSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">totalSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">validate</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">full</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 第二个参数用于删除目标集合（如果存在）</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">renameCollection</span><span class="token punctuation">(</span><span class="token string">"new_coll"</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="索引"><a aria-hidden="true" tabindex="-1" href="#索引"><span class="icon icon-link"></span></a>索引</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表索引"><a aria-hidden="true" tabindex="-1" href="#列表索引"><span class="icon icon-link"></span></a>列表索引</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">getIndexes</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">getIndexKeys</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-3"><div class="wrap-header h3wrap"><h3 id="创建索引"><a aria-hidden="true" tabindex="-1" href="#创建索引"><span class="icon icon-link"></span></a>创建索引</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-3-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line"><span class="token comment">// 索引类型</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>                <span class="token comment">// 单字段索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-property property">"date"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>     <span class="token comment">// 复合索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token property">foo</span><span class="token operator">:</span> <span class="token string">"text"</span><span class="token punctuation">,</span> <span class="token property">bar</span><span class="token operator">:</span> <span class="token string">"text"</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 文本索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"$**"</span><span class="token operator">:</span> <span class="token string">"text"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>            <span class="token comment">// 通配符文本索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"userMetadata.$**"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>    <span class="token comment">// 通配符索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"loc"</span><span class="token operator">:</span> <span class="token string">"2d"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>              <span class="token comment">// 二维索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"loc"</span><span class="token operator">:</span> <span class="token string">"2dsphere"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>        <span class="token comment">// 2dsphere 索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token string">"hashed"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>          <span class="token comment">// 哈希索引</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Index Options</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"lastModifiedDate"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">expireAfterSeconds</span><span class="token operator">:</span> <span class="token number">3600</span><span class="token punctuation">}</span><span class="token punctuation">)</span>      <span class="token comment">// TTL指数</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">unique</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">partialFilterExpression</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">age</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property"><span class="token keyword">$gt</span></span><span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 部分索引</span>
</span><span class="code-line"><span class="token comment">// 强度为 1 或 2 的不区分大小写的索引</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">collation</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token property">locale</span><span class="token operator">:</span> <span class="token string">'en'</span><span class="token punctuation">,</span> <span class="token property">strength</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">createIndex</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"name"</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">sparse</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除索引"><a aria-hidden="true" tabindex="-1" href="#删除索引"><span class="icon icon-link"></span></a>删除索引</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">dropIndex</span><span class="token punctuation">(</span><span class="token string">"name_1"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="隐藏取消隐藏索引"><a aria-hidden="true" tabindex="-1" href="#隐藏取消隐藏索引"><span class="icon icon-link"></span></a>隐藏/取消隐藏索引</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">hideIndex</span><span class="token punctuation">(</span><span class="token string">"name_1"</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">unhideIndex</span><span class="token punctuation">(</span><span class="token string">"name_1"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="方便的命令"><a aria-hidden="true" tabindex="-1" href="#方便的命令"><span class="icon icon-link"></span></a>方便的命令</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-3" style="padding-top: 0;"><div class="wrap-header h3wrap"><h3 style="display:none;" id=""><a aria-hidden="true" tabindex="-1" href="#"><span class="icon icon-link"></span></a></h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3&style=display:none;&wrap-style=padding-top: 0;-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">use admin
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">createUser</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"user"</span><span class="token operator">:</span> <span class="token string">"root"</span><span class="token punctuation">,</span> <span class="token string-property property">"pwd"</span><span class="token operator">:</span> <span class="token function">passwordPrompt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string-property property">"roles"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"root"</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">dropUser</span><span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">auth</span><span class="token punctuation">(</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token function">passwordPrompt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">use test
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getSiblingDB</span><span class="token punctuation">(</span><span class="token string">"dbname"</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">currentOp</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">killOp</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span> <span class="token comment">// opid</span>
</span><span class="code-line">
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">fsyncLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">fsyncUnlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getCollectionNames</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getCollectionInfos</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">printCollectionStats</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">stats</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getReplicationInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">printReplicationInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">isMaster</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">hostInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">printShardingStatus</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">shutdownServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">serverStatus</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">setSlaveOk</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getSlaveOk</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getProfilingLevel</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getProfilingStatus</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">setProfilingLevel</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span> <span class="token comment">// 0 == OFF, 1 == ON with slowms, 2 == ON</span>
</span><span class="code-line">
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">enableFreeMonitoring</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">disableFreeMonitoring</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">getFreeMonitoringStatus</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">db<span class="token punctuation">.</span><span class="token method function property-access">createView</span><span class="token punctuation">(</span><span class="token string">"viewName"</span><span class="token punctuation">,</span> <span class="token string">"sourceColl"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token property"><span class="token keyword">$project</span></span><span class="token operator">:</span><span class="token punctuation">{</span><span class="token property">department</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="各种各样的"><a aria-hidden="true" tabindex="-1" href="#各种各样的"><span class="icon icon-link"></span></a>各种各样的</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="改变流"><a aria-hidden="true" tabindex="-1" href="#改变流"><span class="icon icon-link"></span></a>改变流</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">watchCursor <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token property-access">coll</span><span class="token punctuation">.</span><span class="token method function property-access">watch</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
</span><span class="code-line">   <span class="token punctuation">{</span>
</span><span class="code-line">      <span class="token property"><span class="token keyword">$match</span></span> <span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string-property property">"operationType"</span><span class="token operator">:</span> <span class="token string">"insert"</span> <span class="token punctuation">}</span>
</span><span class="code-line">   <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword control-flow">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>watchCursor<span class="token punctuation">.</span><span class="token method function property-access">isExhausted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
</span><span class="code-line">   <span class="token keyword control-flow">if</span> <span class="token punctuation">(</span>watchCursor<span class="token punctuation">.</span><span class="token method function property-access">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
</span><span class="code-line">      <span class="token function">print</span><span class="token punctuation">(</span><span class="token function">tojson</span><span class="token punctuation">(</span>watchCursor<span class="token punctuation">.</span><span class="token method function property-access">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">   <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-3"><div class="wrap-header h3wrap"><h3 id="分片集群"><a aria-hidden="true" tabindex="-1" href="#分片集群"><span class="icon icon-link"></span></a>分片集群</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-3-->
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">addShard</span><span class="token punctuation">(</span><span class="token string">"rs1/mongodbd1.example.net:27017"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">shardCollection</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">zipcode</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">moveChunk</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token property">zipcode</span><span class="token operator">:</span> <span class="token string">"53187"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">"shard0019"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">splitAt</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">x</span><span class="token operator">:</span> <span class="token number">70</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">splitFind</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">x</span><span class="token operator">:</span> <span class="token number">70</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">disableAutoSplit</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">enableAutoSplit</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">startBalancer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">stopBalancer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">disableBalancing</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">enableBalancing</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">getBalancerState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">setBalancerState</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token operator">/</span><span class="token boolean">false</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">isBalancerRunning</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">addTagRange</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">state</span><span class="token operator">:</span> <span class="token string">"NY"</span><span class="token punctuation">,</span><span class="token property">zip</span><span class="token operator">:</span> <span class="token builtin keyword">MinKey</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">state</span><span class="token operator">:</span> <span class="token string">"NY"</span><span class="token punctuation">,</span><span class="token property">zip</span><span class="token operator">:</span> <span class="token builtin keyword">MaxKey</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">"NY"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">removeTagRange</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">state</span><span class="token operator">:</span> <span class="token string">"NY"</span><span class="token punctuation">,</span><span class="token property">zip</span><span class="token operator">:</span> <span class="token builtin keyword">MinKey</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">state</span><span class="token operator">:</span> <span class="token string">"NY"</span><span class="token punctuation">,</span><span class="token property">zip</span><span class="token operator">:</span> <span class="token builtin keyword">MaxKey</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">"NY"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">addShardTag</span><span class="token punctuation">(</span><span class="token string">"shard0000"</span><span class="token punctuation">,</span> <span class="token string">"NYC"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">removeShardTag</span><span class="token punctuation">(</span><span class="token string">"shard0000"</span><span class="token punctuation">,</span> <span class="token string">"NYC"</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">addShardToZone</span><span class="token punctuation">(</span><span class="token string">"shard0000"</span><span class="token punctuation">,</span> <span class="token string">"JFK"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">removeShardFromZone</span><span class="token punctuation">(</span><span class="token string">"shard0000"</span><span class="token punctuation">,</span> <span class="token string">"NYC"</span><span class="token punctuation">)</span>
</span><span class="code-line">sh<span class="token punctuation">.</span><span class="token method function property-access">removeRangeFromZone</span><span class="token punctuation">(</span><span class="token string">"mydb.coll"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">a</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token property">b</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token property">a</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token property">b</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="副本集"><a aria-hidden="true" tabindex="-1" href="#副本集"><span class="icon icon-link"></span></a>副本集</h3><div class="wrap-body">
<pre class="language-mongodb"><code class="language-mongodb code-highlight"><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">initiate</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string-property property">"_id"</span><span class="token operator">:</span> <span class="token string">"replicaTest"</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token property">members</span><span class="token operator">:</span> <span class="token punctuation">[</span>
</span><span class="code-line">    <span class="token punctuation">{</span> <span class="token property">_id</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token property">host</span><span class="token operator">:</span> <span class="token string">"<span class="token entity" title="127.0.0.1">127.0.0.1</span>:27017"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token punctuation">{</span> <span class="token property">_id</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token property">host</span><span class="token operator">:</span> <span class="token string">"<span class="token entity" title="127.0.0.1">127.0.0.1</span>:27018"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token punctuation">{</span> <span class="token property">_id</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token property">host</span><span class="token operator">:</span> <span class="token string">"<span class="token entity" title="127.0.0.1">127.0.0.1</span>:27019"</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token property">arbiterOnly</span><span class="token operator">:</span><span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">add</span><span class="token punctuation">(</span><span class="token string">"mongodbd1.example.net:27017"</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">addArb</span><span class="token punctuation">(</span><span class="token string">"mongodbd2.example.net:27017"</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">remove</span><span class="token punctuation">(</span><span class="token string">"mongodbd1.example.net:27017"</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">conf</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">isMaster</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">printReplicationInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">printSlaveReplicationInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">reconfig</span><span class="token punctuation">(</span><span class="token operator">&#x3C;</span>valid_conf<span class="token operator">></span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">slaveOk</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">rs<span class="token punctuation">.</span><span class="token method function property-access">stepDown</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// (stepDownSecs, secondaryCatchUpPeriodSecs)</span>
</span></code></pre>
</div></div></div></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="..\/data.js?v=1.4.1" defer></script><script src="..\/js/fuse.min.js?v=1.4.1" defer></script><script src="..\/js/main.js?v=1.4.1" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
